//
//  ViewController.m
//  MusicPlayer
//
//  Created by ZEROTECH on 2017/7/26.
//  Copyright © 2017年 ZEROTECH. All rights reserved.
//

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()
{
    
}

@property(nonatomic,strong) AVAudioPlayer *player;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //后台播放音频设置
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setActive:YES error:nil];
    [session setCategory:AVAudioSessionCategoryPlayback error:nil];
    
    //让app支持接受远程控制事件
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    
    //播放背景音乐
    //NSString *musicPath = [[NSBundle mainBundle] pathForResource:@"Folk Song" ofType:@"mp3"];
    //NSURL *url = [[NSURL alloc] initFileURLWithPath:musicPath];
    
    NSURL *url = [[NSURL alloc] initWithString:@"https://git.oschina.net/yww1984/music/raw/master/MusicPlayer/MusicPlayer/Folk%20Song.mp3"];
    // 创建播放器
    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
   
    [self.player prepareToPlay];
    [self.player setVolume:1];
    self.player.numberOfLoops = -1; //设置音乐播放次数  -1为一直循环
    [self.player play]; //播放
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
